﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace UFSControl
{

    public enum ServiceType : int
    {
        TV = 1,
        Radio = 2
    }

    public enum Polarisation : int
    {
        V = 1,
        H = 2
    }

    public class KService
    {  
        private int _ChannelNo;
        private string _Name;
        private string _Sid;
        private string _Tsid;
        private string _Onid;
        KCarrier _Carrier;
        private string _Vpid;
        private string _Apid;
        private string _PCRpid;
        private bool _Scrambled;
        private bool _Skipped;
        private bool _Locked;
        private ServiceType _KServiceType; //TV, Radio

        public int ChannelNo { get { return _ChannelNo;} set { _ChannelNo = value;} }
        public string Name { get { return _Name; } set { _Name = value; } }
        public string Sid { get { return _Sid; } set { _Sid = value; } }
        public string Tsid { get { return _Tsid; } set { _Tsid = value; } }
        public string Onid { get { return _Onid; } set { _Onid = value; } }
        public KCarrier Carrier { get { return _Carrier; } set { _Carrier = value; } } 
        public string Vpid { get { return _Vpid; } set { _Vpid = value; } }
        public string Apid { get { return _Apid; } set { _Apid = value; } }
        public string PCRpid { get { return _PCRpid; } set { _PCRpid = value; } }
        public bool Scrambled { get { return _Scrambled; } set { _Scrambled = value; } }
        public bool Skipped { get { return _Skipped; } set { _Skipped = value; } }
        public bool Locked { get { return _Locked; } set { _Locked = value; } }
        public ServiceType KServiceType { get { return _KServiceType; } set { _KServiceType = value; } }

        public KService() 
        {
            Carrier = new KCarrier();
        }

        public KService(int AChannelNo, string AName, string ASid, string ATsid, string AOnid, KCarrier ACarrier,
                        string AVpid, string AApid, string APCRpid, bool AScrambled, bool ASkipped, bool ALocked, ServiceType AKServiceType)
        {
            _ChannelNo = AChannelNo;
            _Name = AName;
            _Sid = ASid;
            _Tsid = ATsid;
            _Onid = AOnid;
            _Carrier = ACarrier;
            _Vpid = AVpid;
            _Apid = AApid;
            _PCRpid = APCRpid;
            _Scrambled = AScrambled;
            _Skipped = ASkipped;
            _Locked = ALocked;
            _KServiceType = AKServiceType;
        }

        public string[] Data()
        {
            string[] lData = new string[11];  
            lData[0] = ChannelNo.ToString();
            lData[1] = Name;
            lData[2] = Carrier.Frequency.ToString();
            lData[3] = Scrambled.ToString();
            lData[4] = Sid;
            lData[5] = Onid;
            lData[6] = Tsid;
            lData[7] = Vpid;
            lData[8] = PCRpid;
            lData[9] = Skipped.ToString();
            lData[10] = Locked.ToString();
           return lData; 
        }

        //XML Node erzeugen
        public XmlNode ToXML(XmlDocument doc)
        {
            XmlNode lElem = doc.CreateElement("Service");

            lElem.AppendChild(doc.CreateElement("ChannelNo")).InnerText = _ChannelNo.ToString();
            lElem.AppendChild(doc.CreateElement("Name")).InnerText = _Name;
            lElem.AppendChild(doc.CreateElement("Sid")).InnerText = _Sid;
            lElem.AppendChild(doc.CreateElement("Tsid")).InnerText = _Tsid;
            lElem.AppendChild(doc.CreateElement("Onid")).InnerText = _Onid;
            lElem.AppendChild(_Carrier.ToXML(doc));
            lElem.AppendChild(doc.CreateElement("Vpid")).InnerText = _Vpid;
            lElem.AppendChild(doc.CreateElement("Apid")).InnerText = _Apid;
            lElem.AppendChild(doc.CreateElement("PCRpid")).InnerText = _PCRpid;
            lElem.AppendChild(doc.CreateElement("Scrambled")).InnerText = _Scrambled.ToString().ToLower();
            lElem.AppendChild(doc.CreateElement("Skipped")).InnerText = _Skipped.ToString().ToLower();
            lElem.AppendChild(doc.CreateElement("Locked")).InnerText = _Locked.ToString().ToLower();
            lElem.AppendChild(doc.CreateElement("ServiceType")).InnerText = (KServiceType == ServiceType.TV) ? "TV" : "Radio";
            return lElem;
        }
    }

    public class KCarrier
    {
      private int _Longitude;
      private string _Band;
      private int _Frequency;
      private int _SymbolRate;
      private Polarisation _Pol; // H, V 
      private string _FEC; //DVBS2 8PSK 2/3

      public int Longitude { get { return Longitude; } set { _Longitude = value; } }
      public string Band { get { return _Band; } set { _Band = value; } }
      public int Frequency { get { return _Frequency; } set { _Frequency = value; } }
      public int SymbolRate { get { return _SymbolRate; } set { _SymbolRate = value; } }
      public Polarisation Pol { get { return _Pol; } set { _Pol = value; } }
      public string FEC { get { return _FEC; } set { _FEC = value; } }

      public KCarrier()
      {
      }

      public KCarrier(int ALongitude, string ABand, int AFrequency, int ASymbolRate, Polarisation APol, string AFEC)
      {
          _Longitude = ALongitude;
          _Band = ABand;
          _Frequency = AFrequency;
          _SymbolRate = ASymbolRate;
          _Pol = APol; 
          _FEC = AFEC; 
      }

        //XML Node erzeugen
      public XmlNode ToXML(XmlDocument doc)
      {
          XmlNode lElem = doc.CreateElement("Carrier");
          lElem.AppendChild(doc.CreateElement("Longitude")).InnerText = _Longitude.ToString();
          lElem.AppendChild(doc.CreateElement("Band")).InnerText = _Band;
          lElem.AppendChild(doc.CreateElement("Frequency")).InnerText = _Frequency.ToString();
          lElem.AppendChild(doc.CreateElement("SymbolRate")).InnerText = _SymbolRate.ToString();
          lElem.AppendChild(doc.CreateElement("Pol")).InnerText = (_Pol == Polarisation.H) ? "H" : "V";
          lElem.AppendChild(doc.CreateElement("FEC")).InnerText = _FEC;
          return lElem;
      }
    }

    public class KTransponder
    {
       private string _Frequency;
       private string _SymbolRate;
       private string _Onid;
       private string _Tsid;
       private string _Nid;
       private Polarisation _Pol; // H, V 
       private string _FEC; // Auto


       public string Frequency { get { return _Frequency; } set { _Frequency = value; } }
       public string SymbolRate { get { return _SymbolRate; } set { _SymbolRate = value; } }
       public string Onid { get { return _Onid; } set { _Onid = value; } }
       public string Tsid { get { return _Tsid; } set { _Tsid = value; } }
       public string Nid { get { return _Nid; } set { _Nid = value; } }
       public Polarisation Pol { get { return _Pol; } set { _Pol = value; } }
       public string FEC { get { return _FEC; } set { _FEC = value; } }

      public KTransponder()
      {
      }

      public KTransponder(string AFrequency, string ASymbolRate, string AOnid, string ATsid, string ANid, Polarisation APol, string AFEC)
      {
          _Frequency = AFrequency;
          _SymbolRate = ASymbolRate;
          _Onid = AOnid;
          _Tsid = ATsid;
          _Nid = ANid;
          _Pol = APol;
          _FEC = AFEC;
      }

      //XML Node erzeugen
      public XmlNode ToXML(XmlDocument doc)
      {
          XmlNode lElem = doc.CreateElement("Transponder");

          lElem.AppendChild(doc.CreateElement("Frequency")).InnerText = _Frequency;
          lElem.AppendChild(doc.CreateElement("SymbolRate")).InnerText = _SymbolRate;
          lElem.AppendChild(doc.CreateElement("Onid")).InnerText = _Onid;
          lElem.AppendChild(doc.CreateElement("Tsid")).InnerText = _Tsid;
          lElem.AppendChild(doc.CreateElement("Nid")).InnerText = _Nid;
          lElem.AppendChild(doc.CreateElement("Pol")).InnerText = (Pol == Polarisation.H) ? "H" : "V";
          lElem.AppendChild(doc.CreateElement("FEC")).InnerText = _FEC;
          return lElem;
      }
    }

    public class KSatellite
    {
        private string _SatName; // SIRIUS 4
        private string _Longitude; // 50
        private string _Band; // Ku
        private List<KTransponder> Transponder;

        public string SatName { get { return _SatName; } set { _SatName = value; } }
        public string Longitude { get { return _Longitude; } set { _Longitude = value; } }
        public string Band { get { return _Band; } set { _Band = value; } }

        public KSatellite()
        {
            Transponder = new List<KTransponder>();
        }

        public void AddTransponder(string AFrequency, string ASymbolRate, string AOnid, string ATsid, string ANid, Polarisation APol, string AFEC)
        {
            Transponder.Add(new KTransponder(AFrequency, ASymbolRate, AOnid, ATsid, ANid, APol, AFEC)); 

        }
        
        //XML Node erzeugen
        public XmlNode ToXML(XmlDocument doc)
        {
            XmlNode lElem = doc.CreateElement("Satellite");

            lElem.AppendChild(doc.CreateElement("SatName")).InnerText = _SatName;
            lElem.AppendChild(doc.CreateElement("Longitude")).InnerText = _Longitude;
            lElem.AppendChild(doc.CreateElement("Band")).InnerText = _Band;
            //alle Transponder einfügen
            foreach (KTransponder Trans in Transponder)
            {
                lElem.AppendChild(Trans.ToXML(doc));
            }
            return lElem;
        }

    }

    public class KFavoriteGroup
    {
        private string _Name;
        private ServiceType _KServiceType; //TV, Radio
        private List<Int32> ChannelNo;

        public string Name { get { return _Name; } set { _Name = value; } }
        public ServiceType KServiceType { get { return _KServiceType; } set { _KServiceType = value; } }
        public List<Int32> Channels{ get {return ChannelNo;}}

        public KFavoriteGroup()
        {
            ChannelNo = new List<Int32>();
        }

        public void AddChannel(Int32 AChannelNo)
        {
            ChannelNo.Add(AChannelNo);

        }

        public string[] Data()
        {
            string[] lData = new string[2];
            lData[0] = Name;
            lData[1] = "";
            return lData;
        }

        //XML Node erzeugen
        public XmlNode ToXML(XmlDocument doc)
        {
            XmlNode lElem = doc.CreateElement("FavoriteGroup");

            lElem.AppendChild(doc.CreateElement("Name")).InnerText = _Name;
            lElem.AppendChild(doc.CreateElement("ServiceType")).InnerText = (_KServiceType == ServiceType.TV) ? "TV" : "Radio";
            foreach (Int32 lChannel in ChannelNo)
            {
                lElem.AppendChild(doc.CreateElement("ChannelNo")).InnerText = lChannel.ToString();
            }
            return lElem;
        }
    }

    public class KChannelFile
    {
        private List<KSatellite> _KSatellite;
        private List<KFavoriteGroup> _KFavoriteGroup;
        private List<KService> _KService;
        private List<KConfiguration> _KConfiguration;

        public List<KService> KService { get { return _KService; } }
        public KChannelFile(){
            _KSatellite = new List<KSatellite>();
            _KFavoriteGroup = new List<KFavoriteGroup>();
            _KService = new List<KService>();
            _KConfiguration = new List<KConfiguration>();
        }

        public void AddSatellite(KSatellite Satellite)
        {
            _KSatellite.Add(Satellite);
        }

        public void AddService(KService Service)
        {
            _KService.Add(Service);
        }

        public void AddFavorite(KFavoriteGroup Favorite)
        {
            _KFavoriteGroup.Add(Favorite);
        }

        public void AddConfiguration(KConfiguration Configuration)
        {
            _KConfiguration.Add(Configuration);
        }

        //XML Dokument erstellen
        public XmlDocument ToXML(){
            XmlDocument doc = new XmlDocument();
            //Create an XML declaration. 
            XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", null, null); //("1.0", "utf-8", "yes")
            XmlNode lRoot;                             
            lRoot = doc.CreateElement("Servicelist");   
            doc.AppendChild(lRoot);
            doc.InsertBefore(xmldecl, lRoot);
            //Liste der Satelliten erzeugen
            XmlNode lSatellitelist = doc.CreateElement("Satellitelist");
            lRoot.AppendChild(lSatellitelist);
            foreach (KSatellite Sat in _KSatellite)
            {
                lSatellitelist.AppendChild(Sat.ToXML(doc));
            }
            //Liste der Konfigurationen
            foreach (KConfiguration Config in _KConfiguration)
            {
                lSatellitelist.AppendChild(Config.ToXML(doc));
            }
            //Liste der Sender erzeugen
            foreach (KService Service in _KService)
            {
                lRoot.AppendChild(Service.ToXML(doc));
            }
            //Liste der Favoriten
            XmlNode lFavoriteList = doc.CreateElement("Favoritelist");
            lRoot.AppendChild(lFavoriteList);
            foreach (KFavoriteGroup Favorite in _KFavoriteGroup)
            {
                lFavoriteList.AppendChild(Favorite.ToXML(doc));
            }
            return doc;
        }
    }

     public class KConfiguration{
         private string _Slot;
         private string _Tuner;
         private string _Longitude;
         private string _Band;
         private string _LO1Frequency;
         private string _LO2Frequency;
         private string _BandSwitchFreq;
         private string _DiSEqC;

         public string Slot { get { return _Slot; } set { _Slot = value; } }
         public string Tuner { get { return _Tuner; } set { _Tuner = value; } }
         public string Longitude { get { return _Longitude; } set { _Longitude = value; } }
         public string Band { get { return _Band; } set { _Band = value; } }
         public string LO1Frequency { get { return _LO1Frequency; } set { _LO1Frequency = value; } }
         public string LO2Frequency { get { return _LO2Frequency; } set { _LO2Frequency = value; } }
         public string BandSwitchFreq { get { return _BandSwitchFreq; } set { _BandSwitchFreq = value; } }
         public string DiSEqC { get { return _DiSEqC; } set { _DiSEqC = value; } }
         
         public KConfiguration()
         {
         }

         //XML Node erzeugen
         public XmlNode ToXML(XmlDocument doc)
         {
             XmlNode lElem = doc.CreateElement("Configuration");

             lElem.AppendChild(doc.CreateElement("Slot")).InnerText = _Slot;
             lElem.AppendChild(doc.CreateElement("Tuner")).InnerText = _Tuner;
             lElem.AppendChild(doc.CreateElement("Longitude")).InnerText = _Longitude;
             lElem.AppendChild(doc.CreateElement("Band")).InnerText = _Band;
             lElem.AppendChild(doc.CreateElement("LO1Frequency")).InnerText = _LO1Frequency;
             lElem.AppendChild(doc.CreateElement("LO2Frequency")).InnerText = _LO2Frequency;
             lElem.AppendChild(doc.CreateElement("BandSwitchFreq")).InnerText = _BandSwitchFreq;
             lElem.AppendChild(doc.CreateElement("DiSEqC")).InnerText = _DiSEqC;
             return lElem;
         }
     }

}
